/*  Copyright 2010 princehaku
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  Author     : princehaku
 */

package net.techest.httpprotocols;

import java.util.HashMap;
import java.util.Map;

/**HTTP请求
 * 
 * @author princehaku
 *
 */
public class HTTPRequest implements HTTPHead,HTTPContext,HTTPStatuLine {
	
		Map<String,String> headers;
		
		public HTTPRequest (){
			headers=new HashMap<String,String>();
		}
		
		private byte[] context="".getBytes();

		private String statuLine="";
	
		/* (non-Javadoc)
		 * @see net.techest.httpprotocols.HTTPHead#setParam(java.lang.String, java.lang.String)
		 */
		@Override
		public void setParam(String key,String value){
				if(value==null){
					headers.remove(key);
				}else{
					headers.put(key, value);
				}
		}
		
		/* (non-Javadoc)
		 * @see net.techest.httpprotocols.HTTPHead#getParam(java.lang.String)
		 */
		@Override
		public String getParam(String key){
				return headers.get(key);
		}
		
		@Override
		public String toString(){
			StringBuilder content=new StringBuilder();
			StringBuilder paragraph=new StringBuilder("");
			//组合请求
			content.append(getStatuLine()+"\n");
			//组合头
			for(String key:this.headers.keySet()){
				paragraph.append(key+":"+this.headers.get(key)+"\n");
			}
			if(!paragraph.equals("")){
				content.append(paragraph.toString());
				content.append("\n");
			}
			//组合正文
			if(!getContext().equals("")){
				content.append(getContext()+"\n");
				content.append("\n");
			}
			
			return content.toString();
		}


		/* (non-Javadoc)
		 * @see net.techest.httpprotocols.HTTPContext#getContext()
		 */
		@Override
		public byte[] getContext() {
			return this.context;
		}

		@Override
		public void setContext(byte[] context) {
			this.context=context;
		}

		/* (non-Javadoc)
		 * @see net.techest.httpprotocols.HTTPStatuLine#getContext()
		 */
		@Override
		public String getStatuLine() {
			return this.statuLine;
		}

		@Override
		public void setStatuLine(String statuLine) {
			this.statuLine=statuLine;
		}
}
